package com.phiture.erp.produce.web.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.phiture.erp.produce.core.pojo.dto.ErpMaterialRequirementPlanSaveReqDTO;
import com.phiture.erp.produce.core.pojo.dto.ErpMaterialRequirementPlanUpdateStatusDTO;
import com.phiture.erp.produce.core.pojo.dto.MrpCalculationDTO;
import com.phiture.erp.produce.core.pojo.vo.ErpMaterialRequirementPlanItemVO;
import com.phiture.erp.produce.core.service.ErpMaterialRequirementPlanService;
import com.phiture.erp.produce.core.service.MrpCalculationService;
import com.phiture.erp.produce.dal.entity.ErpMaterialRequirementPlanDO;
import com.phiture.erp.produce.dal.pojo.dto.ErpMaterialRequirementPlanPageReqDTO;
import com.phiture.erp.produce.dal.pojo.vo.ErpMaterialRequirementPlanRespVO;
import com.phiture.erp.produce.dal.pojo.vo.ErpMaterialRequirementPlanVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 物料需求计划")
@RestController
@RequiredArgsConstructor
@RequestMapping("/erp/material-requirement-plan")
public class ErpMaterialRequirementPlanController {

    private final ErpMaterialRequirementPlanService materialRequirementPlanService;
    private final MrpCalculationService mrpCalculationService;

//    @PostMapping("/create")
//    @Operation(summary = "创建物料需求计划")
//    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:create')")
//    public CommonResult<Long> createMaterialRequirementPlan(@Valid @RequestBody ErpMaterialRequirementPlanSaveReqDTO createReqDTO) {
//        return success(materialRequirementPlanService.createMaterialRequirementPlan(createReqDTO));
//    }


    @PostMapping("/calculation")
    @Operation(summary = "MRP运算")
    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:calculation')")
    public CommonResult<Boolean> mrpCalculation(@RequestBody MrpCalculationDTO dto) {
        mrpCalculationService.materialMrpCalculation(dto);
        return success(Boolean.TRUE);
    }

    @PostMapping("/mrpTaskDispatch")
    @Operation(summary = "MRP任务下达")
    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:mrpTaskDispatch')")
    public CommonResult<Boolean> mrpTaskDispatch(@RequestBody Long id) {
        mrpCalculationService.materialMrpTaskDispatch(Collections.singletonList(id), false);
        return success(Boolean.TRUE);
    }

    @PostMapping("/mrpBatchTaskDispatch")
    @Operation(summary = "MRP任务下达")
    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:mrpTaskDispatch')")
    public CommonResult<Boolean> mrpBatchTaskDispatch(@RequestBody List<Long> ids) {
        mrpCalculationService.materialMrpTaskDispatch(ids, true);
        return success(Boolean.TRUE);
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料需求计划")
    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:update')")
    public CommonResult<Boolean> updateMaterialRequirementPlan(@Valid @RequestBody ErpMaterialRequirementPlanSaveReqDTO updateReqDTO) {
        materialRequirementPlanService.updateMaterialRequirementPlan(updateReqDTO);
        return success(true);
    }

    @PutMapping("/mrpApproval")
    @Operation(summary = "MRP审批")
    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:update')")
    public CommonResult<Boolean> mrpApproval(@Valid @RequestBody ErpMaterialRequirementPlanUpdateStatusDTO updateReqDTO) {
        materialRequirementPlanService.updateStatus(updateReqDTO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料需求计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:delete')")
    public CommonResult<Boolean> deleteMaterialRequirementPlan(@RequestParam("id") Long id) {
        materialRequirementPlanService.deleteMaterialRequirementPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料需求计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:query')")
    public CommonResult<ErpMaterialRequirementPlanRespVO> getMaterialRequirementPlan(@RequestParam("id") Long id) {
        ErpMaterialRequirementPlanDO materialRequirementPlan = materialRequirementPlanService.getMaterialRequirementPlan(id);
        return success(BeanUtils.toBean(materialRequirementPlan, ErpMaterialRequirementPlanRespVO.class));
    }

    @GetMapping("/getInfo")
    @Operation(summary = "获得物料需求计划详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:query')")
    public CommonResult<ErpMaterialRequirementPlanVO> getInfo(@RequestParam("materialRequirementPlanId") Long materialRequirementPlanId) {
        ErpMaterialRequirementPlanVO planVO = materialRequirementPlanService.getInfoById(materialRequirementPlanId);
        return success(planVO);
    }

    @GetMapping("/getItemInfo")
    @Operation(summary = "获得物料需求计划详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:query')")
    public CommonResult<List<ErpMaterialRequirementPlanItemVO>> getItemInfo(@RequestParam("materialRequirementPlanId") Long materialRequirementPlanId) {
        List<ErpMaterialRequirementPlanItemVO> itemInfo = materialRequirementPlanService.getItemInfo(Collections.singletonList(materialRequirementPlanId));
        return success(itemInfo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料需求计划分页")
    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:query')")
    public CommonResult<PageResult<ErpMaterialRequirementPlanRespVO>> getMaterialRequirementPlanPage(@Valid ErpMaterialRequirementPlanPageReqDTO pageReqDTO) {
        return success(materialRequirementPlanService.getMaterialRequirementPlanPage(pageReqDTO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料需求计划 Excel")
    @PreAuthorize("@ss.hasPermission('erp:material-requirement-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialRequirementPlanExcel(@Valid ErpMaterialRequirementPlanPageReqDTO pageReqDTO,
                                                   HttpServletResponse response) throws IOException {
        pageReqDTO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpMaterialRequirementPlanRespVO> list = materialRequirementPlanService.getMaterialRequirementPlanPage(pageReqDTO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料需求计划.xls", "数据", ErpMaterialRequirementPlanRespVO.class, list);
    }
}